﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.Text;

using Bussiness_Objects;

namespace DataObjects.ADO
{
    public class SqlThamSo : IThamSoDao
    {
        DataTable detail;        

        public SqlThamSo()
        {
            detail = GetTable();
            KiemTraNamXuatBan_Error = "Năm xuất bản phải trong vòng " + detail.Rows[3]["GiaTri"].ToString() + " năm";
            KiemTraTuoi_Error = "Tuổi độc giả phải từ " + detail.Rows[0]["GiaTri"].ToString() + " đến " + detail.Rows[1]["GiaTri"].ToString();
        }

        #region property value
        public int tuoiToiThieu 
        {
            get
            {
                return int.Parse(detail.Rows[0]["GiaTri"].ToString());
            }
        }
        public int tuoiToiDa
        {
            get
            {
                return int.Parse(detail.Rows[1]["GiaTri"].ToString());
            }
        }
        public int thoiHanThe
        {
            get
            {
                return int.Parse(detail.Rows[2]["GiaTri"].ToString());
            }
        }
        public int khoangCachNamXB
        {
            get
            {
                return int.Parse(detail.Rows[3]["GiaTri"].ToString());
            }
        }
        public int soNgayMuon
        {
            get
            {
                return int.Parse(detail.Rows[4]["GiaTri"].ToString());
            }
        }
        public int soSachMuon
        {
            get
            {
                return int.Parse(detail.Rows[5]["GiaTri"].ToString());
            }
        }
        public int tienPhat
        {
            get
            {
                return int.Parse(detail.Rows[6]["GiaTri"].ToString());
            }
        }
        public int apDungQD6
        {
            get
            {
                return int.Parse(detail.Rows[7]["GiaTri"].ToString());
            }
        }
#endregion

        public DataTable GetTable()
        {
            string sqlString = "sp_THAMSO_selectAll";
            return Db.Read(sqlString,null);
        }

        public void UpdateValue()
        {
            detail = GetTable();
        }

        public string Update(object businessData)
        {
            ThamSo _thamso = businessData as ThamSo;
            if (_thamso != null)
            {
                _thamso.SetParameterUpdate();
                string sqlString = "sp_THAMSO_update";
                return Db.ExecuteStoreProcedure(sqlString, _thamso.parms);
            }
            return "Error";
        }

        public string Update(string parName, int value)
        {
            object[] parms = new object[2];
            SqlParameter _par = new SqlParameter("@TenThamSo", parName);
            parms[0] = _par;

            _par = new SqlParameter("@GiaTri", value);
            parms[1] = _par;           

            return Db.ExecuteStoreProcedure("sp_THAMSO_update", parms);            
        }

        public string Insert(object businessData)
        {
            return null;
        }

        public bool KiemTraTuoi(string ngaySinh)
        {
            object[] parms = new object[2];
            SqlParameter para = new SqlParameter("@NgaySinh", ngaySinh);
            parms[0] = para;

            para = new SqlParameter();
            para.ParameterName = "@result";
            para.Direction = ParameterDirection.Output;
            para.DbType = DbType.Int16;
            para.Value = 0;
            parms[1] = para;

            Db.ExecuteStoreProcedure("sp_THAMSO_kiemtraTuoi", parms);
            if ((Int16)((SqlParameter)parms[1]).Value == 1)
                return true;
            else
                return false;
        }

        public string KiemTraTuoi_Error { get; set; }

        public string ThoiHanTHe(string ngayLT)
        {
            object[] parms = new object[2];
            SqlParameter para = new SqlParameter("@NgayLapThe", ngayLT);
            parms[0] = para;

            para = new SqlParameter();
            para.ParameterName = "@NgayHetHan";
            para.Direction = ParameterDirection.Output;
            para.DbType = DbType.DateTime;            
            parms[1] = para;

            Db.ExecuteStoreProcedure("sp_THAMSO_tinhThoiHanThe", parms);            
            return ((DateTime)para.Value).ToShortDateString();
        }

        public bool KiemTraNamXuatBan(int namXB)
        {
            object[] parms = new object[2];
            SqlParameter para = new SqlParameter("@NamXuatBan", namXB);
            parms[0] = para;

            para = new SqlParameter();
            para.ParameterName = "@result";
            para.Direction = ParameterDirection.Output;
            para.DbType = DbType.Int16;
            para.Value = 0;
            parms[1] = para;

            Db.ExecuteStoreProcedure("sp_THAMSO_kiemtraNXBSach", parms);
            if ((Int16)((SqlParameter)parms[1]).Value == 1)
                return true;
            else
                return false;
        }

        public string KiemTraNamXuatBan_Error { get; set; }
    }
}
